/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.sdawd
 */
package Client;

import java.util.HashMap;
import java.util.logging.Level;
import java.util.logging.Logger;

import Games.GamesController;

import ui.Main;

/**
 *
 * @author Brandon
 */
public class Functions extends Thread {
        
    public Functions() {

    }
    
    @Override
    public void run() {
        while(Comm.connected) {
            try {
                synchronized(Command.comm.queue) {
                    if(Command.comm.queue.isEmpty())
                        Command.comm.queue.wait();
                    execute(Command.comm.queue.poll());
                }
            } catch (InterruptedException ex) {
                Logger.getLogger(Functions.class.getName()).log(Level.SEVERE, null, ex);
            }
        }
    }

    private void execute(HashMap<String, String[]> function) {
        if(function != null) {
        	if(function.containsKey("LOGIN"))
        	{
        		String[] values = function.get("LOGIN");
        		if(values[0] == "OK"){
        			Main.actionAfterLogin(true, null);        			
        		}else{
        			Main.actionAfterLogin(false, null);
        		}
        		Command.setLastCommand(null);
        		
        	}else if(function.containsKey("GAMELIST")) {
        		String[] games =  function.get("GAMELIST");
        		for(int i = 0; i < games.length; i++ ){
        			Main.addServerGames(games[i], i);
        		}

            } else if(function.containsKey("MOVE")) {
                //Receive a move
                String[] details = function.get("MOVE");
                GamesController.placePiece(details[0], Integer.parseInt(details[2]));
                if(details[0].equals(GamesController.gamesModel().getUs())) {
                    GamesController.gamesModel().setCurrentPlayer(GamesController.gamesModel().getThem());
                    GamesController.setCurrentPlayer(GamesController.gamesModel().getThem());
                } else if (Main.ai) {
                	GamesController.game.opponentMove(Integer.parseInt(details[2]));
                }
                GamesController.updateScore();
            }else if(function.containsKey("YOURTURN")){
                GamesController.appendHistory("It's our turn.");
                GamesController.gamesModel().setCurrentPlayer(GamesController.gamesModel().getUs());
                GamesController.setCurrentPlayer(GamesController.gamesModel().getUs());
                if(Main.ai) {
                    //Do AI move
                    GamesController.game.chooseMove(GamesController.gamesModel().getPlayersHashMap().get(GamesController.gamesModel().getUs()));
                    GamesController.appendHistory("The AI made a move");
                }
            }else if(function.containsKey("MATCH")){
                //Accept a match
                String[] details = function.get("MATCH");
                String player2;
                if (GamesController.gamesModel().getPlayerOne().equals(details[1])) {
                	player2 = details[2];
                        GamesController.gamesModel().setThem(details[2]);
                	GamesController.gamesModel().setUs(details[1]) ;
                        GamesController.appendHistory("We have the first turn.");
                } else {
                	player2 = GamesController.gamesModel().getPlayerOne();
                	GamesController.gamesModel().setUs(GamesController.gamesModel().getPlayerOne());
                        GamesController.gamesModel().setThem(details[1]);
                        GamesController.appendHistory("The opponent has the first turn.");
                }
                
                String player1 = details[1];
                GamesController.gamesModel().setPlayerOne(player1);
                GamesController.gamesModel().setPlayerTwo(player2);
                
                GamesController.setPlayerOne(player1);
                GamesController.setPlayerTwo(player2);
                GamesController.gamesModel().setCurrentPlayer(player1);
                GamesController.setInitialPlayer(player1);
                GamesController.updateScore();
                Main.received = true;
                
            }else if(function.containsKey("WIN")){
                Main.addWin();
                GamesController.showDialog("WIN","You have won this game!");
            }else if(function.containsKey("LOSS")){
                Main.addLoss();
                GamesController.showDialog("LOSS","You have lost THE GAME!");
            }else if(function.containsKey("DRAW")){
                Main.addDraw();
                GamesController.showDialog("DRAW","This game has ended in a draw!");
            }else if(function.containsKey("ERR") && Command.getLastCommand().equals("login")){
                String[] details = function.get("ERR");
                Main.actionAfterLogin(false, details[0]);
                Command.setLastCommand(null);
            } else
                //throw new UnsupportedOperationException("This function is not yet implemented");
                    System.out.println(function.toString());
        }
    }
}
